import router from "../router";
import localStorage from "../utilsCustomize/localStorage";
import {isEmpty} from "element-ui/src/utils/util";

export default {
    state: {
        routerListState: [],
        isCollapseState: false,
        projectNameState: 'AdminVueThinkPHP',
        elIconSFoldUnfoldIconState: 'el-icon-s-fold',
        clickMenuState: '',
    },
    mutations: {
        menuListMutations(state) {

            let adminRules = localStorage.get('rules', true);

            if (!adminRules || state.routerListState.length !== 0) return true;

            let pathRouter = '',
                nameRouter = '',
                routerList = [{
                    path: "/",
                    name: "Index",
                    component: () => import("../views/home/Index"),
                    children: []
                }];



            adminRules.forEach(function(v) {

                if (v.pid !== 0) {

                    nameRouter = v.path.split('/').pop();
                    pathRouter = nameRouter.charAt(0).toLowerCase() + nameRouter.slice(1);

                    routerList[0]['children'].push({
                        path: `/${pathRouter}`,
                        name: nameRouter,
                        component: () => import(`../views/${v.path}.vue`)
                    });

                } else {
                    routerList[0]['children'].push({
                        path: `/${v.name}`,
                        name: v.name,
                        component: () => import(`../views/${v.path}`)
                    });
                }
            });

            state.routerListState = routerList;

            router.addRoutes(state.routerListState);
        },
        collapseMenuMutations(state) {
            state.isCollapseState = !state.isCollapseState;

            if (state.isCollapseState === true) {
                state.projectNameState = 'Admin';
                state.elIconSFoldUnfoldIconState = 'el-icon-s-unfold';
            } else {
                state.projectNameState = 'AdminVueThinkPHP';
                state.elIconSFoldUnfoldIconState = 'el-icon-s-fold';
            }

        },
        clickMenuMutations(state, value) {

            if (typeof value === 'object' && value !== null) value = value.pop();

            state.clickMenuState = value;
        },
        rulesMutations(state, value) {

            let menuList = [];

            value.forEach(function(v) {

                if (v.pid !== 0 || v.enable !== 2 || v.menu !== 2) return true;

                v.children = [];

                menuList.push(v);
            });

            if (!isEmpty(menuList)) {
                menuList.forEach(function(v, k) {
                    value.forEach(function(v2) {

                        if (v.id !== v2.pid || v2.enable !== 2 || v2.menu !== 2) return true;

                        menuList[k]['children'].push(v2);
                    });
                });
            }

            localStorage.set('menuList', menuList);
            localStorage.set('rules', value);

            this.commit('menuListMutations');
        }

    }
};
